package com.yanqu.road.dao.impl.activity.commonteam;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamApplyData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonTeamApplyDataDaoImpl extends TempDataObjectDao<CommonTeamApplyData> {

    public Map<Integer, Map<Long, CommonTeamApplyData>> getCommonTeamApplyDataMap(int activityId, int groupId, String teamIds) {
        String getCommonTeamApplyDataSql = "select * from `t_c_common_team_apply` where `activityId`=? and groupId = ? and delFlag = 0 and teamId in (" + teamIds + ")";
        Map<Integer, Map<Long, CommonTeamApplyData>> map = new ConcurrentHashMap<>();
        if (StringUtils.isNullOrEmpty(teamIds)) {
            return map;
        }
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getCommonTeamApplyDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonTeamApplyData data = parse(rs);
                Map<Long, CommonTeamApplyData> teamApplyMap = map.computeIfAbsent(data.getTeamId(), k -> new ConcurrentHashMap<>());
                teamApplyMap.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateCommonTeamApplyDataSql = "update `t_c_common_team_apply` set userName=?,serverId=?,score=?,delFlag=?,createTime=? where activityId = ? and groupId = ? and teamId = ? and userId = ?";
    @Override
    protected boolean updateData(CommonTeamApplyData commonTeamApplyData) {
        commonTeamApplyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamApplyData.getUserName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getServerId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getUserId()));
        if (getDb().executeNoneQuery(updateCommonTeamApplyDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCommonTeamApplyDataSql = "insert into `t_c_common_team_apply`(activityId,groupId,userId,userName,serverId,teamId,score,delFlag,createTime) values (?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CommonTeamApplyData commonTeamApplyData) {
        commonTeamApplyData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, commonTeamApplyData.getUserName()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getScore()));
        para.put(i++, new DbParameter(Types.INTEGER, commonTeamApplyData.getDelFlag()));
        para.put(i++, new DbParameter(Types.BIGINT, commonTeamApplyData.getCreateTime()));
        if (getDb().executeNoneQuery(insertCommonTeamApplyDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CommonTeamApplyData parseData(ResultSet rs) throws SQLException {
        CommonTeamApplyData commonTeamApplyData = new CommonTeamApplyData();
        commonTeamApplyData.setActivityId(rs.getInt("activityId"));
        commonTeamApplyData.setGroupId(rs.getInt("groupId"));
        commonTeamApplyData.setUserId(rs.getLong("userId"));
        commonTeamApplyData.setUserName(rs.getString("userName"));
        commonTeamApplyData.setServerId(rs.getLong("serverId"));
        commonTeamApplyData.setTeamId(rs.getInt("teamId"));
        commonTeamApplyData.setScore(rs.getLong("score"));
        commonTeamApplyData.setDelFlag(rs.getInt("delFlag"));
        commonTeamApplyData.setCreateTime(rs.getLong("createTime"));
        return commonTeamApplyData;
    }
}
